package com.zhugang.week12;

import java.util.HashSet;
import java.util.Set;

/**
 * @program algorithms
 * @description: lengthOfLongestSubstring
 * @author: chanzhugang
 * @create: 2022/09/07 19:27
 */
public class LengthOfLongestSubstring {

    /**
     * 剑指offer 48 最长不含重复字符的子字符串
     *
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        // 最长不重复子字符串的长度
        // 滑动窗口
        int p = 0;
        int q = 0;
        Set<Character> set = new HashSet<>();
        int maxL = 0;
        while (q < s.length()) {
            char c = s.charAt(q);
            if (!set.contains(c)) {
                // 不包含重复元素
                set.add(c);
                q++;
                maxL = Math.max(maxL, q - p);
                continue;
            }
            while (set.contains(c)) {
                // 包含重复元素，一直删除   abc b 删除到b
                set.remove(s.charAt(p));
                p++;
            }
        }
        return maxL;

    }
}